package com.cloud.system.project.controller;

import com.cloud.common.core.enums.AuthEnums;
import com.cloud.common.core.model.response.ApiResponses;
import com.cloud.common.core.model.supers.SuperController;
import com.cloud.common.security.annotation.PreAuth;
import com.cloud.common.security.utils.ApiUtils;
import com.cloud.system.project.model.parm.LoginPARM;
import com.cloud.system.project.service.ITokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 登录 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2021-01-04
 */
@Api(tags = {"登录"})
@RestController
public class TokenRestController extends SuperController {

    @Autowired
    private ITokenService tokenService;

    @PreAuth(AuthEnums.PC_LOGIN)
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ApiResponses<String> login(@RequestBody @Validated LoginPARM loginPARM) {
        String token = tokenService.login(loginPARM.getLoginName(), loginPARM.getPassword());
        return success(token);
    }

    @PreAuth
    @ApiOperation("用户注销")
    @PostMapping("/logout")
    public ApiResponses<Void> logout() {
        tokenService.logout(ApiUtils.getToken());
        return success();
    }

    @PreAuth
    @ApiOperation(value = "token刷新", notes = "一定要携带没过期的token才能获取新的token")
    @PostMapping("/refresh")
    public ApiResponses<String> refresh() {
        String newToken = tokenService.refresh();
        return success(newToken);
    }


}

